package org.infodavid.common.persistence;

import java.util.Locale;

import javax.persistence.PersistenceException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.infodavid.common.model.ITemplateVersion;
import org.infodavid.common.persistence.impl.AbstractDataService;
import org.infodavid.common.reflect.InstantiationHelper;

/**
 * The Class TemplateVersionDataService.
 */
public abstract class TemplateVersionDataService extends AbstractDataService<ITemplateVersion,Long> {

  /** The singleton. */
  private static final TemplateVersionDataService SINGLETON;

  /** The Constant log. */
  private static final Log LOGGER = LogFactory.getLog(TemplateVersionDataService.class);

  static {
    try {
      SINGLETON = InstantiationHelper.getInstance().newInstance(TemplateVersionDataService.class);
    }
    catch (final Exception e) {
      LOGGER.fatal("Can not instantiate the " + TemplateVersionDataService.class.getName(), e);

      throw new Error(e);
    }
  }

  /**
   * returns the singleton.
   * @return the singletons
   */
  public static TemplateVersionDataService getInstance() {
    return SINGLETON;
  }

  /**
   * Instantiates a new service.
   */
  protected TemplateVersionDataService() {
    super();
  }

  /**
   * Find last version of the template.
   * @param session the session
   * @param templateKey the template key
   * @return the template version
   * @throws PersistenceException the persistence exception
   */
  public abstract ITemplateVersion findLastVersion(PersistenceSession session, Long templateKey)
      throws PersistenceException;

  /**
   * Find last revision of the template.
   * @param session the session
   * @param templateKey the template key
   * @return the revision as integer
   * @throws PersistenceException the persistence exception
   */
  public abstract Integer findLastRevision(PersistenceSession session, Long templateKey)
      throws PersistenceException;

  /**
   * Find last version by name.
   * @param session the session
   * @param value the value
   * @param language the language
   * @return the template version
   * @throws PersistenceException the persistence exception
   */
  public abstract ITemplateVersion findLastVersionByName(PersistenceSession session, String value,
      Locale language) throws PersistenceException;
}
